package com.baoli.blzj.adapter.home;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoli.blzj.bean.home.HomeMenuBean;
import com.bumptech.glide.Glide;
import com.baoli.blzj.MainActivity;
import com.baoli.blzj.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 保利推荐适配器 by John on 2017/6/29.
 */

public class HomeGnAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private Context mContext;
    private List<HomeMenuBean> datas;
    private LayoutInflater inflater;
    private int screenWidth = 0;

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view);

        void onItemLongClick(View view);
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setList(List<HomeMenuBean> hBean) {
        datas.clear();
        append(hBean);
    }

    public void append(List<HomeMenuBean> hBean) {
        datas.addAll(hBean);
        notifyDataSetChanged();
    }


    public HomeGnAdapter(Context context) {
        mContext = context;
        this.datas = new ArrayList<>();
        this.inflater = (LayoutInflater) LayoutInflater.from(context).getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics metric = new DisplayMetrics();
        ((MainActivity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
        this.screenWidth = metric.widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = inflater.inflate(R.layout.item_teshu_grid, parent,
                    false);
            MyViewHolder holder = new MyViewHolder(view);

            view.setOnClickListener(this);
            view.setOnLongClickListener(this);

            return holder;
        } else {
            View v = inflater.inflate(R.layout.item_page_small, parent, false);
            MyViewHolder2 holder2 = new MyViewHolder2(v);

            v.setOnClickListener(this);
            v.setOnLongClickListener(this);
            return holder2;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            Glide.with(mContext)
                    .load(datas.get(position).getUrl())
                    .crossFade()
                    .animate(R.anim.anim_photo)
                    .placeholder(R.drawable.ic_launcher)
                    .error(R.drawable.ic_launcher)
                    .into(((MyViewHolder) holder).iv);
            ((MyViewHolder) holder).title.setText(datas.get(position).getTitle());
        } else if (holder instanceof MyViewHolder2) {
            ((MyViewHolder2) holder).tv.setText(datas.get(position).getTitle());
            Glide.with(mContext)
                    .load(datas.get(position).getImg())
                    .crossFade()
                    .animate(R.anim.anim_photo)
                    .placeholder(R.drawable.ic_launcher)
                    .error(R.drawable.ic_launcher)
                    .into(((MyViewHolder2) holder).img);
            /*Glide.with(mContext)
                    .load(datas.get(position).getBg())
                    .crossFade()
                    .animate(R.anim.anim_photo)
                    .placeholder(R.drawable.fuwupingjia)
                    .error(R.drawable.fuwupingjia)
                    .into(((MyViewHolder2) holder).bg_layout);*/
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {

        //判断item是图还是显示页数（图片有URL）
        if (!TextUtils.isEmpty(datas.get(position).getUrl())) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {

            mOnItemClickListener.onItemClick(v);
        }

    }


    @Override
    public boolean onLongClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemLongClick(v);
        }
        return false;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView title;
        private LinearLayout layout;

        public MyViewHolder(View view) {
            super(view);
            iv = (ImageView) view.findViewById(R.id.iv);
            title = (TextView) view.findViewById(R.id.title);
            layout = (LinearLayout) view.findViewById(R.id.layout);

            StaggeredGridLayoutManager.LayoutParams linearParams = (StaggeredGridLayoutManager.LayoutParams) layout.getLayoutParams();

            linearParams.width = screenWidth / 2;
            linearParams.height = screenWidth / 4;

            layout.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        private TextView tv;
        private ImageView img;
        private ImageView bg_layout;
        private RelativeLayout big_layout;

        public MyViewHolder2(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv);
            img = (ImageView) view.findViewById(R.id.img);
            bg_layout = (ImageView) view.findViewById(R.id.bg_layout);
            big_layout = (RelativeLayout) view.findViewById(R.id.big_layout);

            StaggeredGridLayoutManager.LayoutParams linearParams = (StaggeredGridLayoutManager.LayoutParams) big_layout.getLayoutParams();

            linearParams.width = screenWidth / 2;
            linearParams.height = screenWidth / 2;//为适应第一个图为正方形,纵跨两行

            big_layout.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }
    }

}
